package Server.Model.Hall;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import Server.Agent.GameAgent;
import common.routine.GlobalSetting;
import common.routine.JsonUtils;
import common.routine.CommonDef.NodeType;
import net.sf.json.JSONObject;
import redis.protocal.DbInfoMgr;

public class NotifyMgr {

	public long m_ggStartTime = 0;
	public String m_ggTxt = "";
	public String m_ggType = "";
	public List<GongGaoPack> m_currGGList = new ArrayList<GongGaoPack>();
	public List<GongGaoPack> m_randGGList = new ArrayList<GongGaoPack>();
	
	private static NotifyMgr _instance = null;
	public static NotifyMgr getInstance(){
		if(_instance == null){
			_instance = new NotifyMgr();
			_instance.init();
		}
		return _instance;
	}
	public void init(){
		if(GlobalSetting.getInstance().getSelfNodeInfo().nodeType == NodeType.LOGIC){
			startGGSend();
		}
	}

	public void startGGSend(){
		class SendGGThread extends TimerTask{
			@Override
			public void run() {
				if(m_ggStartTime != 0){//有重启公告不做处理
					return;
				}
				if(m_currGGList.size() != 0){
					GongGaoPack gg = m_currGGList.get(0);
					gg.currnum++;
					CMD_S_AgentGG cmd = new CMD_S_AgentGG();
					cmd.GGtxt = gg.txt;
					cmd.GGtype = gg.type;
					String sData = cmd.getJsonData();
					GameAgent.Broadcast(sData);	
					if(gg.currnum >= gg.maxnum){
						m_randGGList.add(gg);
						m_currGGList.remove(0);
					}
				}else if(m_randGGList.size() != 0){
					Random rand = new Random();
					int r = rand.nextInt(100) + 1;
					if(r <= 20){
						if(r <= 10){
							int rindex = rand.nextInt(m_randGGList.size());
							GongGaoPack gg = m_randGGList.get(rindex);
							gg.currnum++;
							CMD_S_AgentGG cmd = new CMD_S_AgentGG();
							cmd.GGtxt = gg.txt;
							cmd.GGtype = gg.type;
							String sData = cmd.getJsonData();
							GameAgent.Broadcast(sData);
						}else{
							String data = DbInfoMgr.getInstance().getRandClubBoradCastGG();
							if(data == null)
								return;
							GongGaoPack gg = JsonUtils.deserialize(data, GongGaoPack.class);
							gg.currnum++;
							CMD_S_AgentGG cmd = new CMD_S_AgentGG();
							cmd.GGtxt = gg.txt;
							cmd.GGtype = gg.type;
							String sData = cmd.getJsonData();
							GameAgent.Broadcast(sData);
						}
					}
				}else{
					Random rand = new Random();
					int r = rand.nextInt(100) + 1;
					if(r <= 20){
						String data = DbInfoMgr.getInstance().getRandClubBoradCastGG();
						if(data == null)
							return;
						GongGaoPack gg = JsonUtils.deserialize(data, GongGaoPack.class);
						gg.currnum++;
						CMD_S_AgentGG cmd = new CMD_S_AgentGG();
						cmd.GGtxt = gg.txt;
						cmd.GGtype = gg.type;
						String sData = cmd.getJsonData();
						GameAgent.Broadcast(sData);
					}
				}
			}
		}
		Timer timer = new Timer();
		SendGGThread task = new SendGGThread();
		timer.schedule(task,1000 * 20,1000 * 20);
	}
	public void handSendGG(String type,String text,String time){
		if(type.equals("1")){
			m_ggType = type;
			m_ggTxt = text;
			m_ggStartTime = System.currentTimeMillis() / 1000;
			CMD_S_AgentGG cmd = new CMD_S_AgentGG();
			cmd.GGtxt = text;
			cmd.GGtype = type;
			cmd.timeGg = 30;
			String sData = cmd.getJsonData();
			GameAgent.Broadcast(sData);
		}else{
			if(type.equals("2")){
				CMD_S_AgentGG cmd = new CMD_S_AgentGG();
				cmd.GGtxt = text;
				cmd.GGtype = type;
				String sData = cmd.getJsonData();
				GameAgent.Broadcast(sData);				
			}else{
				GongGaoPack gg = new GongGaoPack();
				gg.type = type;
				gg.txt = text;
				gg.time = time;
				if(gg.time.equals("0") || gg.time.equals("")){
					gg.maxnum = 1;
					gg.currnum = 0;
				}else{
					gg.maxnum = Integer.parseInt(gg.time);
					gg.currnum = 0;
				}
				m_currGGList.add(gg);
			}
		}
	}
	//客户端来回切换
	public void Handle_OnGGTime(GameAgent agent,JSONObject root){
		long nowTime = System.currentTimeMillis() / 1000;
		long difTime = nowTime - m_ggStartTime;
		if(m_ggType.equals("1") && difTime < 1800){
			CMD_S_RenewGGTime cmd = new CMD_S_RenewGGTime();
			cmd.re_time = 30-difTime/60;
			String sData = cmd.getJsonData();
			GameAgent.Broadcast(sData);
		}
	}
	//客户端登录
	public void Handle_OnClientLogin(int lUid){
		if(m_ggStartTime != 0 && m_ggType.equals("1")){
			long m_nowTime = System.currentTimeMillis() / 1000;
			if((m_nowTime - m_ggStartTime)<1800){
				CMD_S_AgentGG ggcmd = new CMD_S_AgentGG();
				ggcmd.GGtxt = m_ggTxt;
				ggcmd.GGtype = m_ggType;
				ggcmd.timeGg = 30-(m_nowTime - m_ggStartTime)/60;
				String ggData = ggcmd.getJsonData();
				dthall.getInstance().onSend(lUid, ggData);
			}else{
				m_ggStartTime = 0;
				m_ggType = "";
				m_ggTxt = "";
			}
		}
	}
}
